/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sheresam.springhbnjpa.test2;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

public class AddJFreeChartPieToPDF {

    public void createPDFFromImage(String outputFile) throws IOException, COSVisitorException {
        // the document
        PDDocument doc = null;
        try {
            doc = new PDDocument();

            PDPage page = new PDPage();
            doc.addPage(page);

            JFreeChart chart = AddJFreeChartPieToPDF.generatePieChart2d();

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ChartUtilities.writeChartAsJPEG(out, chart, 300, 300);//write to outstream
            //create a new inputstream
            InputStream in = new ByteArrayInputStream(out.toByteArray());
            
            PDXObjectImage ximage = new PDJpeg(doc, in);

            PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
            
            contentStream.drawImage(ximage, 100, 400);
//            contentStream.drawXObject(ximage, 100, 300, 100, 100);
            
            contentStream.close();
            
            
            // page 2
            page = new PDPage();
            doc.addPage(page);

            chart = AddJFreeChartPieToPDF.generatePieChart3d();

            out = new ByteArrayOutputStream();
            ChartUtilities.writeChartAsJPEG(out, chart, 300, 300);//write to outstream
            //create a new inputstream
            in = new ByteArrayInputStream(out.toByteArray());
            
            ximage = new PDJpeg(doc, in);

            contentStream = new PDPageContentStream(doc, page, true, true);
            
            contentStream.drawImage(ximage, 100, 400);
//            contentStream.drawXObject(ximage, 100, 300, 100, 100);
            
            contentStream.close();
            doc.save(outputFile);
        } finally {
            if (doc != null) {
                doc.close();
            }
        }
    }

    public static void main(String[] args) {
        AddJFreeChartPieToPDF app = new AddJFreeChartPieToPDF();
        try {
            if (args.length != 1) {
                app.usage();
            } else {
                app.createPDFFromImage(args[0]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This will print out a message telling how to use this example.
     */
    private void usage() {
        System.err.println("usage: " + this.getClass().getName() + " <output-pdf>");
    }

    public static JFreeChart generatePieChart2d() {
        DefaultPieDataset dataSet = new DefaultPieDataset();
        dataSet.setValue("China", 19.64);
        dataSet.setValue("India", 17.3);
        dataSet.setValue("United States", 4.54);
        dataSet.setValue("Indonesia", 3.4);
        dataSet.setValue("Brazil", 2.83);
        dataSet.setValue("Pakistan", 2.48);
        dataSet.setValue("Bangladesh", 2.38);

        JFreeChart chart = ChartFactory.createPieChart("World Population by countries", dataSet, true, true, false);
        
        return chart;
    }
    
    public static JFreeChart generatePieChart3d() {
        DefaultPieDataset dataSet = new DefaultPieDataset();
        dataSet.setValue("China", 19.64);
        dataSet.setValue("India", 17.3);
        dataSet.setValue("United States", 4.54);
        dataSet.setValue("Indonesia", 3.4);
        dataSet.setValue("Brazil", 2.83);
        dataSet.setValue("Pakistan", 2.48);
        dataSet.setValue("Bangladesh", 2.38);

        JFreeChart chart = ChartFactory.createPieChart3D("World Population by countries", dataSet, true, true, false);
        
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);

        return chart;
    }

    public static void copyStream(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[1024]; // Adjust if you want
        int bytesRead;
        while ((bytesRead = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
    }
}
